<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>爱健康</title>
    <meta name="description" content="爱健康">
    <meta name="keywords" content="爱健康">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
    <!-- 引入组件库 -->
    <script type="text/javascript" src="../js/jquery.min.js"></script>
    <script src="../js/vue.js"></script>
    <script src="../plugins/elementui/index.js"></script>
    <script src="../js/axios-0.18.0.js"></script>
    <style>
        .datatable {
            position: relative;
            box-sizing: border-box;
            -webkit-box-flex: 1;
            width: 100%;
            max-width: 100%;
            font-size: 14px;
            color: rgb(96, 98, 102);
            overflow: hidden;
            flex: 1 1 0%;
        }
        .datatable td, .datatable th {
            padding: 12px 0;
            min-width: 0;
            -webkit-box-sizing: border-box;
            box-sizing: border-box;
            text-overflow: ellipsis;
            vertical-align: middle;
            position: relative;
            text-align: left;
        }
    </style>
</head>
<body class="hold-transition">
    <div id="app">
        <div class="content-header">
            <h1>会员管理</h1>
            <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
                <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
                <el-breadcrumb-item>会员管理</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="app-container">
            <div class="box">
                <div class="filter-container">
                    <el-input placeholder="档案号/手机号/姓名" v-model="pagination.queryString" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" clearable></el-input>
                    <el-button @click="handleFilter()"  class="dalfBut">查询</el-button>
                </div>
                <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                    <el-table-column type="index" align="center" label="序号"></el-table-column>
                    <el-table-column prop="fileNumber" label="档案号" align="center"></el-table-column>
                    <el-table-column prop="name" label="姓名" align="center"></el-table-column>
                    <el-table-column prop="sex" label="性别" align="center">
                        <template slot-scope="scope">
                            {{ scope.row.sex === "1" ? '女' : '男' }}
                        </template>
                    </el-table-column>
                    <el-table-column prop="phoneNumber" label="手机号" align="center"></el-table-column>
                    <el-table-column prop="regTime" label="注册时间" align="center" :formatter="formatRegTime"></el-table-column>
                    <el-table-column label="操作" align="center">
                        <template slot-scope="scope">
                            <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                            <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
                <div class="pagination-container">
                    <el-pagination
                        class="pagiantion"
                        @current-change="handleCurrentChange"
                        :current-page="pagination.currentPage"
                        :page-size="pagination.pageSize"
                        layout="total, prev, pager, next, jumper"
                        :total="pagination.total">
                    </el-pagination>
                </div>
            </div>
        </div>
        <!-- 添加修改人员列表对话框 -->
        <el-dialog title="编辑会员信息" :visible.sync="dialogFormVisible">
            <el-form :model="editForm">
                <el-form-item label="档案号" label-width="120px">
                    <el-input v-model="editForm.fileNumber" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="姓名" label-width="120px">
                    <el-input v-model="editForm.name" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="性别" label-width="120px">
                    <el-select v-model="editForm.sex" placeholder="请选择性别">
                        <el-option label="男" value="0"></el-option>
                        <el-option label="女" value="1"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="手机号" label-width="120px">
                    <el-input v-model="editForm.phoneNumber" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="注册时间" label-width="120px">
                    <el-input v-model="editForm.regTime" autocomplete="off" disabled></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="submitForm">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</body>

<script>
    var vue = new Vue({
        el: '#app',
        data: {
            pagination: { //分页相关属性
                currentPage: 1,
                pageSize: 10,
                total: 0,
                queryString: null,
            },
            dataList: [], //列表数据
            dialogFormVisible: false, // 编辑表单是否显示
            editForm: { // 编辑表单数据
                id: null,
                fileNumber: '',
                name: '',
                sex: '',
                phoneNumber: '',
                regTime: ''
            }
        },
        created() {
            this.findPage();
        },
        methods: {
            //分页查询
            findPage() {
                //封装分页查询条件
                var param = {
                    currentPage: this.pagination.currentPage,
                    pageSize: this.pagination.pageSize,
                    queryString: this.pagination.queryString,
                };
                // 发送ajax请求，进行分页查询
                axios.get("/adminMember/findPage.do", { params: param }).then((res) => {
                    this.dataList = res.data.rows;
                    this.pagination.total = res.data.total;
                });
            },
            //切换页码
            handleCurrentChange(currentPage) {
                this.pagination.currentPage = currentPage;
                this.findPage();
            },
            // 删除
            handleDelete(row) {
                // 添加确认对话框
                this.$confirm('确定要删除吗？', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    // 调用删除接口
                    axios.delete("/adminMember.do", { params: { id: row.id } }).then((res) => {
                        if (res.data.flag) {
                            this.$message.success(res.data.message); // 弹窗提示删除成功
                            this.findPage(); // 刷新列表
                        } else {
                            this.$message.error(res.data.message); // 弹窗提示删除失败
                        }
                    }).catch((error) => {
                        this.$message.error('删除失败: ' + error);
                    });
                }).catch(() => {
                    this.$message.info('已取消删除');
                });
            },
            // 编辑
            handleUpdate(row) {
                // 先向MemberController中的findById()发送请求
                axios.get("/adminMember/findById.do", { params: { id: row.id } }).then((res) => {
                    console.log("Fetched data:", res.data); // 添加调试信息
                    this.editForm = res.data.data;
                    // 格式化注册时间为年月日
                    this.editForm.regTime = this.editForm.regTime.split(' ')[0];
                    this.dialogFormVisible = true;
                }).catch((error) => {
                    console.error("Error fetching data:", error); // 添加错误处理
                });
            },
            // 提交编辑
            submitForm() {
                // 向edit()发送请求修改数据
                axios.put("/adminMember.do", this.editForm).then((res) => {
                    if (res.data.flag) {
                        this.$message.success(res.data.message); // 弹窗提示编辑成功
                        this.dialogFormVisible = false;
                        this.findPage(); // 刷新列表
                    } else {
                        this.$message.error(res.data.message); // 弹窗提示编辑失败
                    }
                });
            },
            // 查询
            handleFilter() {
                this.pagination.currentPage = 1; // 重置当前页为第一页
                this.findPage();
            },
            formatRegTime(row) {
                const date = new Date(row.regTime);
                const year = date.getFullYear();
                const month = String(date.getMonth() + 1).padStart(2, '0');
                const day = String(date.getDate()).padStart(2, '0');
                return `${year}-${month}-${day}`;
            }
        }
    })
</script>